package com.taobao.pet.service.impl;
import com.taobao.basic.service.impl.BaseServiceImpl;
import com.taobao.pet.domain.Pet;
import com.taobao.pet.domain.PetDetail;
import com.taobao.pet.mapper.PetDetailMapper;
import com.taobao.pet.mapper.PetMapper;
import com.taobao.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public void save(Pet pet) {
        // 这里先添加pet，返回自增的主键id
        petMapper.save(pet);

        // 再添加t_petDetail
        PetDetail petDetail = pet.getPetDetail();
        petDetail.setPet_id(pet.getId());
        petDetailMapper.save(petDetail);

    }

    @Override
    public void update(Pet pet) {
        // 获取pet对象数据
        petMapper.update(pet);
        // 再获取t_petDetail数据
        petDetailMapper.update(pet.getPetDetail());
    }

    /**
     * 宠物列表关联删除宠物详情
     * @param id
     */
    @Override
    public void remove(Long id) {

        petMapper.remove(id);

        petDetailMapper.removePetDetailByPetId(id);
    }

    @Override
    public void onSale(List<Long> ids) {
        Date date = new Date();

        petMapper.onSale(ids,date);


    }

    @Override
    public void offSale(List<Long> ids) {

        Date date = new Date();

        petMapper.offSale(ids,date);

    }

}
